home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / text / faqs / motif-faq.part3 < prev    next >
Encoding:
Internet Message Format  |  1994-04-16  |  49.5 KB

  1. Subject: Motif FAQ (Part 3 of 5)
  2. Newsgroups: comp.windows.x.motif,news.answers,comp.answers
  3. From: dealy@kong.gsfc.nasa.gov (Brian Dealy)
  4. Date: 14 Apr 1994 15:20:35 -0400
  5.  
  6. Archive-name: motif-faq/part3
  7. Last-modified: APR 04, 1994
  8. Version: 3.6
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. -----------------------------------------------------------------------------
  17. Subject: 62)  TOPIC: FORM WIDGET
  18.  
  19.  
  20. -----------------------------------------------------------------------------
  21. Subject: 63)  Why don't labels in a Form resize when the label is changed?
  22. I've got some labels in a form. The labels don't resize whenever the label
  23. string resource is changed. As a result, the operator has to resize the window
  24. to see the new label contents. I am using Motif 1.1.
  25.  
  26. Answer: This problem may happen to any widget inside a Form widget. The
  27. problem was that the Form will resize itself when it gets geometry requests
  28. from its children. If its preferred size is not allowed, the Form will
  29. disallow all geometry requests from its children. The workaround is that you
  30. should set any ancestor of the Form to be resizable. For the shell which
  31. contains the Form you should set the shell resource XmNallowShellResize to be
  32. True (by default, it is set to FALSE).  There is currently an inconsistency on
  33. how resizing is being done, and it may get fixed in Motif 1.2.
  34.  
  35. From db@sunbim.be (Danny Backx)
  36.  
  37. Basically what you have to do is set the XmNresizePolicy on the Form to
  38. XmRESIZE_NONE.  The facts seem to be that XmRESIZE_NONE does NOT mean "do not
  39. allow resizes".  You may also have to set XmNresizable on the form to True.
  40.  
  41. -----------------------------------------------------------------------------
  42. Subject: 64)  How can I center a widget in a form?
  43.  
  44. Answer: One of Motif's trickier questions.  The problems are that: Form gives
  45. no support for centering, only for edge attachments, and the widget must stay
  46. in the center if the form or the widget is resized.  Just looking at
  47. horizontal centering (vertical is similar) some solutions are:
  48.  
  49.  a.  Use the table widget instead of Form.
  50.  
  51.  b.  A hack free solution is from Dan Heller:
  52.  
  53.      /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  54.       * This program is freely distributable without licensing fees and
  55.       * is provided without guarantee or warranty expressed or implied.
  56.       * This program is -not- in the public domain.  This program is
  57.       * taken from the Motif Programming Manual, O'Reilly Volume 6.
  58.       */
  59.  
  60.      /* corners.c -- demonstrate widget layout management for a
  61.       * BulletinBoard widget.  There are four widgets each labeled
  62.       * top-left, top-right, bottom-left and bottom-right.  Their
  63.       * positions in the bulletin board correspond to their names.
  64.       * Only when the widget is resized does the geometry management
  65.       * kick in and position the children in their correct locations.
  66.       */
  67.      #include <Xm/BulletinB.h>
  68.      #include <Xm/PushBG.h>
  69.  
  70.      char *corners[] = {
  71.          "Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right",
  72.      };
  73.  
  74.      static void resize();
  75.  
  76.      main(argc, argv)
  77.      int argc;
  78.      char *argv[];
  79.      {
  80.          Widget toplevel, bboard;
  81.          XtAppContext app;
  82.          XtActionsRec rec;
  83.          int i;
  84.  
  85.          /* Initialize toolkit and create toplevel shell */
  86.          toplevel = XtVaAppInitialize(&app, "Demos", NULL, 0,
  87.              &argc, argv, NULL, NULL);
  88.  
  89.          /* Create your standard BulletinBoard widget */
  90.          bboard = XtVaCreateManagedWidget("bboard",
  91.              xmBulletinBoardWidgetClass, toplevel, NULL);
  92.  
  93.          /* Set up a translation table that captures "Resize" events
  94.           * (also called ConfigureNotify or Configure events).  If the
  95.           * event is generated, call the function resize().
  96.           */
  97.          rec.string = "resize";
  98.          rec.proc = resize;
  99.          XtAppAddActions(app, &rec, 1);
  100.          XtOverrideTranslations(bboard,
  101.              XtParseTranslationTable("<Configure>: resize()"));
  102.  
  103.          /* Create children of the dialog -- a PushButton in each corner. */
  104.          for (i = 0; i < XtNumber(corners); i++)
  105.              XtVaCreateManagedWidget(corners[i],
  106.                  xmPushButtonGadgetClass, bboard, NULL);
  107.  
  108.          XtRealizeWidget(toplevel);
  109.          XtAppMainLoop(app);
  110.      }
  111.  
  112.      /* resize(), the routine that is automatically called by Xt upon the
  113.       * delivery of a Configure event.  This happens whenever the widget
  114.       * gets resized.
  115.       */
  116.      static void
  117.      resize(w, event, args, num_args)
  118.      CompositeWidget w;   /* The widget (BulletinBoard) that got resized */
  119.      XConfigureEvent *event;  /* The event struct associated with the event */
  120.      String args[]; /* unused */
  121.      int *num_args; /* unused */
  122.      {
  123.          WidgetList children;
  124.          int width = event->width;
  125.          int height = event->height;
  126.          Dimension w_width, w_height;
  127.          short margin_w, margin_h;
  128.  
  129.          /* get handle to BulletinBoard's children and marginal spacing */
  130.          XtVaGetValues(w,
  131.              XmNchildren, &children,
  132.              XmNmarginWidth, &margin_w,
  133.              XmNmarginHeight, &margin_h,
  134.              NULL);
  135.  
  136.          /* place the top left widget */
  137.          XtVaSetValues(children[0],
  138.              XmNx, margin_w,
  139.  
  140.              XmNy, margin_h,
  141.              NULL);
  142.  
  143.          /* top right */
  144.          XtVaGetValues(children[1], XmNwidth, &w_width, NULL);
  145.  
  146.          /* To Center a widget in the middle of the BulletinBoard (or Form),
  147.           * simply call:
  148.           *   XtVaSetValues(widget,
  149.                XmNx,    (width - w_width)/2,
  150.                XmNy,    (height - w_height)/2,
  151.                NULL);
  152.           * and return.
  153.           */
  154.          XtVaSetValues(children[1],
  155.              XmNx, width - margin_w - w_width,
  156.              XmNy, margin_h,
  157.              NULL);
  158.          /* bottom left */
  159.          XtVaGetValues(children[2], XmNheight, &w_height, NULL);
  160.          XtVaSetValues(children[2],
  161.  
  162.              XmNx, margin_w,
  163.              XmNy, height - margin_h - w_height,
  164.              NULL);
  165.          /* bottom right */
  166.          XtVaGetValues(children[3],
  167.              XmNheight, &w_height,
  168.              XmNwidth, &w_width,
  169.              NULL);
  170.          XtVaSetValues(children[3],
  171.              XmNx, width - margin_w - w_width,
  172.              XmNy, height - margin_h - w_height,
  173.              NULL);
  174.      }
  175.  
  176.  c.  No uil solution has been suggested, because of the widget size problem
  177.  
  178. -----------------------------------------------------------------------------
  179. Subject: 65)  How do I line up two columns of widgets of different types?  I
  180. have a column of say label widgets, and a column of text widgets and I want to
  181. have them lined up horizontally. The problem is that they are of different
  182. heights. Just putting them in a form or rowcolumn doesn't line them up
  183. properly because the label and text widgets are of different height.
  184.  
  185. If you want the geometry to look like this
  186.  
  187.           -------------------------------------
  188.          |          -------------------------- |
  189.          |a label  |Some text                 ||
  190.          |          -------------------------- |
  191.                            ------------------- |
  192.          |a longer label  |Some more text     ||
  193.          |                 ------------------- |
  194.          |                    ---------------- |
  195.          |a very long label  |Even more text  ||
  196.          |                    ---------------- |
  197.           -------------------------------------
  198.  
  199. try
  200.  
  201. /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  202.  * This program is freely distributable without licensing fees and
  203.  * is provided without guarantee or warranty expressed or implied.
  204.  * This program is -not- in the public domain.  This program is
  205.  * taken from the Motif Programming Manual, O'Reilly Volume 6.
  206.  */
  207.  
  208. /* text_form.c -- demonstrate how attachments work in Form widgets.
  209.  * by creating a text-entry form type application.
  210.  */
  211.  
  212. #include <Xm/PushB.h>
  213. #include <Xm/PushBG.h>
  214. #include <Xm/LabelG.h>
  215. #include <Xm/Text.h>
  216. #include <Xm/Form.h>
  217.  
  218. char *prompts[] = {
  219.     "Name:", "Phone:", "Address:",
  220.     "City:", "State:", "Zip:",
  221. };
  222.  
  223. main(argc, argv)
  224. int argc;
  225. char *argv[];
  226. {
  227.     Widget toplevel, mainform, subform, label, text;
  228.     XtAppContext app;
  229.     char buf[32];
  230.     int i;
  231.  
  232.     toplevel = XtVaAppInitialize(&app, "Demos", NULL, 0,
  233.         &argc, argv, NULL, NULL);
  234.  
  235.     mainform = XtVaCreateWidget("mainform",
  236.         xmFormWidgetClass, toplevel,
  237.         NULL);
  238.  
  239.     for (i = 0; i < XtNumber(prompts); i++) {
  240.         subform = XtVaCreateWidget("subform",
  241.             xmFormWidgetClass,   mainform,
  242.             /* first one should be attached for form */
  243.             XmNtopAttachment,    i? XmATTACH_WIDGET : XmATTACH_FORM,
  244.             /* others are attached to the previous subform */
  245.             XmNtopWidget,        subform,
  246.             XmNleftAttachment,   XmATTACH_FORM,
  247.             XmNrightAttachment,  XmATTACH_FORM,
  248.             NULL);
  249.         label = XtVaCreateManagedWidget(prompts[i],
  250.             xmLabelGadgetClass,  subform,
  251.             XmNtopAttachment,    XmATTACH_FORM,
  252.             XmNbottomAttachment, XmATTACH_FORM,
  253.             XmNleftAttachment,   XmATTACH_FORM,
  254.             XmNalignment,        XmALIGNMENT_BEGINNING,
  255.             NULL);
  256.         sprintf(buf, "text_%d", i);
  257.         text = XtVaCreateManagedWidget(buf,
  258.             xmTextWidgetClass,   subform,
  259.             XmNtopAttachment,    XmATTACH_FORM,
  260.             XmNbottomAttachment, XmATTACH_FORM,
  261.             XmNrightAttachment,  XmATTACH_FORM,
  262.             XmNleftAttachment,   XmATTACH_WIDGET,
  263.             XmNleftWidget,       label,
  264.             NULL);
  265.         XtManageChild(subform);
  266.     }
  267.     /* Now that all the forms are added, manage the main form */
  268.     XtManageChild(mainform);
  269.  
  270.     XtRealizeWidget(toplevel);
  271.     XtAppMainLoop(app);
  272. }
  273.  
  274. If you resize horizontally it stretches the text widgets.  If you resize
  275. vertically it leaves space under the bottom (if you don't resize, this is not
  276. problem).
  277.  
  278. If you want the text widgets to be lined up on the left, as in
  279.  
  280.           ----------------------------------------
  281.          |                    ------------------- |
  282.          |          a label  |Some text          ||
  283.          |                    ------------------- |
  284.                               ------------------- |
  285.          |   a longer label  |Some more text     ||
  286.          |                    ------------------- |
  287.          |                    ------------------- |
  288.          |a very long label  |Even more text     ||
  289.          |                    ------------------- |
  290.           ----------------------------------------
  291.  
  292. try this
  293.  
  294. /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  295.  * This program is freely distributable without licensing fees and
  296.  * is provided without guarantee or warranty expressed or implied.
  297.  * This program is -not- in the public domain.  This program is
  298.  * taken from the Motif Programming Manual, O'Reilly Volume 6.
  299.  */
  300.  
  301. /* text_entry.c -- This demo shows how the RowColumn widget can be
  302.  * configured to build a text entry form.  It displays a table of
  303.  * right-justified Labels and Text widgets that extend to the right
  304.  * edge of the Form.
  305.  */
  306. #include <Xm/LabelG.h>
  307. #include <Xm/RowColumn.h>
  308. #include <Xm/Text.h>
  309.  
  310. char *text_labels[] = {
  311.     "Name:", "Phone:", "Address:", "City:", "State:", "Zip:",
  312. };
  313.  
  314. main(argc, argv)
  315. int argc;
  316. char *argv[];
  317. {
  318.     Widget toplevel, rowcol;
  319.     XtAppContext app;
  320.     char buf[8];
  321.     int i;
  322.  
  323.     toplevel = XtVaAppInitialize(&app, "Demos", NULL, 0,
  324.         &argc, argv, NULL, NULL);
  325.  
  326.     rowcol = XtVaCreateWidget("rowcolumn",
  327.         xmRowColumnWidgetClass, toplevel,
  328.         XmNpacking,        XmPACK_COLUMN,
  329.         XmNnumColumns,     XtNumber(text_labels),
  330.         XmNorientation,    XmHORIZONTAL,
  331.         XmNisAligned,      True,
  332.         XmNentryAlignment, XmALIGNMENT_END,
  333.         NULL);
  334.  
  335.     /* simply loop thru the strings creating a widget for each one */
  336.     for (i = 0; i < XtNumber(text_labels); i++) {
  337.         XtVaCreateManagedWidget(text_labels[i],
  338.             xmLabelGadgetClass, rowcol,
  339.             NULL);
  340.         sprintf(buf, "text_%d", i);
  341.         XtVaCreateManagedWidget(buf,
  342.             xmTextWidgetClass, rowcol,
  343.             NULL);
  344.     }
  345.  
  346.     XtManageChild(rowcol);
  347.     XtRealizeWidget(toplevel);
  348.     XtAppMainLoop(app);
  349. }
  350.  
  351. This makes all objects exactly the same size.  It does not resize in nice
  352. ways.
  353.  
  354. If you want the text widgets lined up on the left, and the labels to be the
  355. size of the longest string, resizing nicely both horizontally and vertically,
  356. as in
  357.  
  358.          -------------------------------------
  359.         |                    ---------------- |
  360.         |          a label  |Some text       ||
  361.         |                    ---------------- |
  362.                              ---------------- |
  363.         |   a longer label  |Some more text  ||
  364.         |                    ---------------- |
  365.         |                    ---------------- |
  366.         |a very long label  |Even more text  ||
  367.         |                    ---------------- |
  368.          -------------------------------------
  369.  
  370.  
  371.  
  372. Answer: Do this: to get the widgets lined up horizontally, use a form but
  373. place the widgets using XmATTACH_POSITION.  In the example, attach the top of
  374. the first label to the form, the bottomPosition to 33 (33% of the height).
  375. Attach the topPosition of the second label to 33 and the bottomPosition to 66.
  376. Attach the topPosition of the third label to 66 and the bottom of the label to
  377. the form.  Do the same with the text widgets.
  378.  
  379. To get the label widgets lined up vertically, use the right attachment of
  380. XmATTACH_OPPOSITE_WIDGET: starting from the one with the longest label, attach
  381. widgets on the right to each other. In the example, attach the 2nd label to
  382. the third, and the first to the second.  To get the text widgets lined up,
  383. just attach them on the left to the labels.  To get the text in the labels
  384. aligned correctly, use XmALIGNMENT_END for the XmNalignment resource.
  385.  
  386.         /* geometry for label 2
  387.         */
  388.         n = 0;
  389.         XtSetArg (args[n], XmNalignment, XmALIGNMENT_END); n++;
  390.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  391.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  392.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  393.         XtSetArg (args[n], XmNtopPosition, 66); n++;
  394.         XtSetValues (label[2], args, n);
  395.  
  396.         /* geometry for label 1
  397.         */
  398.         n = 0;
  399.         XtSetArg (args[n], XmNalignment, XmALIGNMENT_END); n++;
  400.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  401.         XtSetArg (args[n], XmNbottomPosition, 66); n++;
  402.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  403.         XtSetArg (args[n], XmNtopPosition, 33); n++;
  404.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  405.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
  406.         XtSetArg (args[n], XmNrightWidget, label[2]); n++;
  407.         XtSetValues (label[1], args, n);
  408.  
  409.         /* geometry for label 0
  410.         */
  411.         n = 0;
  412.         XtSetArg (args[n], XmNalignment, XmALIGNMENT_END); n++;
  413.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  414.         XtSetArg (args[n], XmNbottomPosition, 33); n++;
  415.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  416.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  417.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
  418.         XtSetArg (args[n], XmNrightWidget, label[1]); n++;
  419.         XtSetValues (label[0], args, n);
  420.  
  421.         /* geometry for text 0
  422.         */
  423.         n = 0;
  424.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  425.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  426.         XtSetArg (args[n], XmNbottomPosition, 33); n++;
  427.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  428.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  429.         XtSetArg (args[n], XmNleftWidget, label[0]); n++;
  430.         XtSetValues (text[0], args, n);
  431.  
  432.         /* geometry for text 1
  433.         */
  434.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  435.         XtSetArg (args[n], XmNtopPosition, 33); n++;
  436.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  437.         XtSetArg (args[n], XmNbottomPosition, 66); n++;
  438.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  439.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  440.         XtSetArg (args[n], XmNleftWidget, label[1]); n++;
  441.         XtSetValues (text[1], args, n);
  442.  
  443.         /* geometry for text 2
  444.         */
  445.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  446.         XtSetArg (args[n], XmNtopPosition, 66); n++;
  447.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  448.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  449.         XtSetArg (args[n], XmNleftWidget, label[2]); n++;
  450.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  451.         XtSetValues (text[2], args, n);
  452.  
  453.  
  454. -----------------------------------------------------------------------------
  455. Subject: 66)  TOPIC: PUSHBUTTON WIDGET
  456.  
  457. -----------------------------------------------------------------------------
  458. Subject: 67)  Why can't I use accelerators on buttons not in a menu?
  459.  
  460. Answer: It is apparently a difficult feature to implement, but OSF are
  461. considering this for the future. It is problematic trying to use the Xt
  462. accelerators since the Motif method interferes with this.  one workaround
  463. suggested is to duplicate your non-menu button by a button in a menu
  464. somewhere, which does have a menu-accelerator installed.  When the user
  465. invokes what they think is the accelerator for the button they can see Motif
  466. actually invokes the button on the menu that they can't see at the time.
  467. Another method is described below and was contributed by Harald Albrecht of
  468. Institute of Geometry and Practical Mathematics Rhine Westphalia Technical
  469. University Aachen (RWTH Aachen), Germany
  470.  
  471.  
  472. From albrecht@igpm.rwth-aachen.de Thu Jul  8 11:44:21 1993
  473.  
  474. My work-around of this problem looks like this: (I've written that code for a
  475. Motif Object Library in C++ so please forgive me for being object orientated!)
  476. The hack consists of a rewritten message loop which checks for keypresses
  477. <MAlt>+<key>. If MessageLoop() finds such a keypress HandleAcc() ist called
  478. and the widget tree is searched for a suitable widget with the right mnemonic.
  479.  
  480.  
  481. // --------------------------------------------------------------------------
  482. // traverse the widget tree starting with the given widget.
  483. //
  484. BOOL TraverseWidgetTree(Widget w, char *pMnemonic, XKeyEvent *KeyEvent)
  485. {
  486.     Widget               wChild;
  487.     WidgetList           ChildList;
  488.     int                  NumChilds, Child;
  489.     KeySym               LabelMnemonic;
  490.     char                 *pMnemonicString;
  491.  
  492. // Check if the widget is a subclass of label -- then it may have an
  493. // accelerator attached...
  494.     if ( XtIsSubclass(w, xmLabelWidgetClass) ) {
  495. // ok. Now: get the widget's mnemonic, convert it to ASCII and compare
  496. // it with the Key we're looking for.
  497.         XtVaGetValues(w, XmNmnemonic, &LabelMnemonic, NULL);
  498.         pMnemonicString = XKeysymToString(LabelMnemonic);
  499.         if ( pMnemonicString &&
  500.              (strcasecmp(pMnemonicString, pMnemonic) == 0) ) {
  501.             // stimulate the keypress
  502.             XmProcessTraversal((Widget)w, XmTRAVERSE_CURRENT);
  503.             KeyEvent->type      = KeyPress;
  504.             KeyEvent->window    = XtWindow(w);
  505.             KeyEvent->subwindow = XtWindow(w);
  506.             KeyEvent->state     = 0;
  507.             KeyEvent->keycode   =
  508.                 XKeysymToKeycode(XtDisplay(w), XK_space);
  509.             XSendEvent(XtDisplay(w), XtWindow(w),
  510.                        True,
  511.                        ButtonPressMask, (XEvent*) KeyEvent);
  512.             KeyEvent->type      = KeyRelease;
  513.             XSendEvent(XtDisplay(w), XtWindow(w),
  514.                        True,
  515.                        ButtonReleaseMask, (XEvent*) KeyEvent);
  516.             return True;
  517.         }
  518.     }
  519. // if this widget is a subclass of Composite check all the widget's
  520. // childs.
  521.     if ( XtIsSubclass(w, compositeWidgetClass) ) {
  522. // if we're in a menu (or something like that) forget this leaf of the
  523. // widget tree!
  524.         if ( XtIsSubclass(w, xmRowColumnWidgetClass) ) {
  525.             unsigned char RowColumnType;
  526.             XtVaGetValues(w, XmNrowColumnType, &RowColumnType, NULL);
  527.             if ( RowColumnType != XmWORK_AREA ) return False;
  528.         }
  529.         XtVaGetValues(w, XmNchildren, &ChildList,
  530.                          XmNnumChildren, &NumChilds, NULL);
  531.         for ( Child = 0; Child < NumChilds; ++Child ) {
  532.             wChild = ChildList[Child];
  533.             if ( TraverseWidgetTree(wChild, pMnemonic, KeyEvent) )
  534.                 return True;
  535.         }
  536.     }
  537.     return False;
  538. } // TraverseWidgetTree
  539. // --------------------------------------------------------------------------
  540. // handle accelerators (keypress MAlt + key)
  541. //
  542. #define MAX_MAPPING 10
  543. BOOL HandleAcc(Widget w, XEvent *event)
  544. {
  545.            Widget         widget, OldWidget;
  546.     static char           keybuffer[MAX_MAPPING];
  547.            int            CharCount;
  548.     static XComposeStatus composeStatus;
  549.  
  550. // convert KeyPress to ASCII
  551.     CharCount = XLookupString((XKeyEvent*) event,
  552.                               keybuffer, sizeof(keybuffer),
  553.                               NULL, &composeStatus);
  554.     keybuffer[CharCount] = 0;
  555. // Only one char is alright -- then search the widget tree for a widget
  556. // with the right mnemonic
  557.     if ( CharCount == 1 ) {
  558.         keybuffer[0] = tolower(keybuffer[0]);
  559.         widget = w;
  560.         while ( (widget != NULL) &&
  561.                 !XtIsSubclass(widget, shellWidgetClass) ) {
  562.             OldWidget = widget; widget = XtParent(widget);
  563.         }
  564.         if ( !widget ) widget = OldWidget;
  565.         return TraverseWidgetTree(widget,
  566.                                   keybuffer, (XKeyEvent*) event);
  567.     }
  568.     return False; // no-one found.
  569. } // HandleAcc
  570. // --------------------------------------------------------------------------
  571. // modified message loop
  572. // loops until the Boolean pFlag points to is set to False
  573. void MessageLoop(Boolean *pFlag)
  574. {
  575.     XEvent nextEvent;
  576.  
  577.     while ( *pFlag ) {
  578.         if ( XtAppPending(AppContext) ) {
  579.             XtAppNextEvent(AppContext, &nextEvent);
  580.             if ( nextEvent.type == KeyPress ) {
  581. // Falls es ein Tastendruck ist, bei dem auch noch die ALT-Taste
  582. // (=Modifier 1) gedrueckt ist, koennte es ein Accelerator sein!
  583.                 if ( nextEvent.xkey.state & Mod1Mask )
  584.                     if ( HandleAcc(XtWindowToWidget(nextEvent.xkey.display,
  585.                                                     nextEvent.xkey.window),
  586.                                    &nextEvent) )
  587.                         continue; // Mitteilung konnte ausgeliefert werden
  588.                                   // und darf daher nicht den ueblichen
  589.                                   // Weg gehen!
  590.             }
  591.             XtDispatchEvent(&nextEvent);
  592.         }
  593.     }
  594. } // TApplication::MessageLoop
  595.  
  596.  
  597. Harald Albrecht albrecht@igpm.rwth-aachen.de Institute of Geometry and
  598. Practical Mathematics Rhine Westphalia Technical University Aachen (RWTH
  599. Aachen), Germany
  600.  
  601.  
  602. -----------------------------------------------------------------------------
  603. Subject: 68)  TOPIC: LABEL WIDGET
  604.  
  605. -----------------------------------------------------------------------------
  606. Subject: 69)  How can I align the text in a label (button, etc) widget?
  607.  
  608. Answer: The alignment for the label widget is controlled by the resource
  609. XmNalignment, and the default centers the text. Use this resource to change it
  610. to left or right alignment.  However, when the label (or any descendant) is in
  611. a row column, and XmNisAligned is True (the default), the row column aligns
  612. text using its resource XmNentryAlignment. If you want simultaneous control
  613. over all widgets use this, but otherwise turn XmNisAligned off and do it
  614. individually.
  615.  
  616.  
  617.  
  618. -----------------------------------------------------------------------------
  619. Subject: 70)  Why doesn't label alignment work in a RowColumn?
  620.  
  621. Answer: RowColumn has a  resource XmNisAligned (default True) and and
  622. XmNentryAlignment (default XmALIGNMENT_BEGINNING).  These control alignment of
  623. the labelString in Labels and descendants. Set XmNisAligned to False to turn
  624. this off.
  625.  
  626. -----------------------------------------------------------------------------
  627. Subject: 71)  How can I set a multiline label?
  628. [Last modified: September 92]
  629.  
  630. Answer: In .Xdefaults
  631.  
  632.       *XmLabel*labelString:             Here\nis\nthe\nLabel
  633.  
  634. This method does not seem to work in some of the older Motif 1.0 versions.
  635.  
  636. In code,
  637.  
  638.       char buf[128];
  639.       XmString msg;
  640.       sprintf(buf, "Here\nis\nthe\nLabel");
  641.       msg = XmStringCreateLtoR(buf, XmSTRING_DEFAULT_CHARSET);
  642.       XtSetArg (args[n], XmNlabelString, msg);
  643.  
  644. Gives a four line label, using the escape sequence \n for a newline.  However,
  645. XmStringCreateLtoR() is obsoleted from version 1.1 on, and may disappear.
  646. This is because it it is only in the AES as "trial-use" and has been proposed
  647. for removal from the AES. Realistically, it will probably not be removed from
  648. any backward compatible versions of Motif, but the potential is there.  If it
  649. does disappear (or if you want to avoid using the non-AES compliant
  650. XmSTRING_DEFAULT_CHARSET), try this from Jean-Philippe Martin-Flatin
  651. <syj@ecmwf.co.uk>
  652.  
  653. #include <Xm/Xm.h>
  654. #include <string.h>
  655.  
  656. /*-----------------------------------------------------
  657.     Create a new XmString from a char*
  658.  
  659.     This function can deal with embedded 'newline' and
  660.     is equivalent to the obsolete XmStringCreateLtoR,
  661.     except it does not use non AES compliant charset
  662.     XmSTRING_DEFAULT_CHARSET
  663. ----------------------------------------------------*/
  664. XmString xec_NewString(char *s)
  665. {
  666.     XmString xms1;
  667.     XmString xms2;
  668.     XmString line;
  669.     XmString separator;
  670.     char     *p;
  671.     char     *t = XtNewString(s);   /* Make a copy for strtok not to */
  672.                                     /* damage the original string    */
  673.  
  674.  
  675.     separator = XmStringSeparatorCreate();
  676.     p         = strtok(t,"\n");
  677.     xms1      = XmStringCreateSimple(p);
  678.  
  679.     while (p = strtok(NULL,"\n"))
  680.     {
  681.         line = XmStringCreateSimple(p);
  682.         xms2 = XmStringConcat(xms1,separator);
  683.         XmStringFree(xms1);
  684.         xms1 = XmStringConcat(xms2,line);
  685.         XmStringFree(xms2);
  686.         XmStringFree(line);
  687.     }
  688.  
  689.     XmStringFree(separator);
  690.     XtFree(t);
  691.     return xms1;
  692. }
  693.  
  694.  
  695. Do not use XmStringCreateSimple() - it does not process the newline character
  696. in the way you want.
  697.  
  698. In UIL, you have to explicitly create a compound string with a separator.
  699. Here's what W. Scott Meeks suggests:
  700.  
  701. value nl : compound_string('', seperate=true);
  702.  
  703. object my_label : XmLabel
  704. {
  705.     arguments
  706.     {
  707.         XmNlabelString = 'Here' & nl & 'is' & nl & 'the' & nl & 'Label';
  708.     };
  709. };
  710.  
  711.  
  712. -----------------------------------------------------------------------------
  713. Subject: 72)  How can I have a vertical label?
  714.  
  715. Answer: Make a multiline label with one character per line, as in the last
  716. question. There is no way to make the text rotated by 90 degrees though.
  717.  
  718.  
  719. -----------------------------------------------------------------------------
  720. Subject: 73)  How can I have a Pixmap in a Label?
  721.  
  722. Answer: From Bob Hays (bobhays@spss.com)
  723.  
  724.     Pixmap px_disarm, px_disarm_insens;
  725.  
  726.     Widget Label1;
  727.     Pixel   foreground, background;
  728.     Arg     args[4];
  729.     Arg     arg[] = {
  730.                 { XmNforeground, &foreground },
  731.                 { XmNbackground, &background }
  732.     };
  733.  
  734.     Label1 = XmCreateLabel ( Shell1, "Label1",
  735.                                        (Arg *) NULL, (Cardinal) 0 );
  736.     XtGetValues ( Label1, arg, XtNumber ( arg ) );
  737.     px_disarm =
  738.       XCreatePixmapFromBitmapData(display,
  739.                                 DefaultRootWindow(display),
  740.                                 mtn_bits, mtn_width, mtn_height,
  741.                                 foreground,
  742.                                 background,
  743.                                 DefaultDepth(display,DefaultScreen(display)));
  744.     px_disarm_insens =
  745.       XCreatePixmapFromBitmapData(display,
  746.                                 DefaultRootWindow(display),
  747.                                 mtn_ins_bits, mtn_ins_width, mtn_ins_height,
  748.                                 foreground,
  749.                                 background,
  750.                                 DefaultDepth(display,DefaultScreen(display)));
  751.  
  752.     n = 0;
  753.     XtSetArg(args[n], XmNlabelType, XmPIXMAP);  n++;
  754.     XtSetArg(args[n], XmNlabelPixmap, px_disarm);  n++;
  755.     XtSetArg(args[n], XmNlabelInsensitivePixmap, px_disarm_insens ); n++;
  756.     XtSetValues ( Label1, args, n );
  757.     XtManageChild(Label1);
  758.  
  759. That will cause the foreground and background of your pixmap to be inherited
  760. from the one that would be used by OSF/Motif when the label is displayed.  The
  761. advantage is that this will utilize any resource values the user may have
  762. requested without looking explicitly into the resource database.  And, you
  763. will have a pixmap handy if the application insensitizes the label (without an
  764. XmNlabelInsensitivePixmap your label will go empty if made insensitive).
  765.  
  766. [Bob's original code was for a PushButton. Just change all Label to PushButton
  767. for them.]
  768.  
  769.  
  770. -----------------------------------------------------------------------------
  771. Subject: 74)  TOPIC: DRAWING AREA WIDGET
  772.  
  773. -----------------------------------------------------------------------------
  774. Subject: 75)  How can I send an expose event to a Drawing Area widget?  (or
  775. any other, come to that). I want to send an expose event so that it will
  776. redraw itself.
  777.  
  778. Answer: Use the Xlib call
  779.  
  780.         XClearArea(XtDisplay(w), XtWindow(w), 0, 0, 0, 0, True)
  781.  
  782. This clears the widget's window and generates an expose event in doing so.
  783. The widgets expose action will then redraw it.  This uses a round trip
  784. request.  An alternative, without the round trip is
  785.  
  786. from orca!mesa!rthomson@uunet.uu.net  (Rich Thomson):
  787.  
  788.     Widget da;
  789.     XmDrawingAreaCallbackStruct da_struct;
  790.  
  791.     da_struct.reason = XmCR_EXPOSE;
  792.     da_struct.event = (XEvent *) NULL;
  793.     da_struct.window = XtWindow(da);
  794.  
  795.     XtCallCallbacks(da, XmNexposeCallback, (XtPointer) da_struct);
  796.  
  797.  
  798. -----------------------------------------------------------------------------
  799. Subject: 76)  How can I know when a DrawingArea has been resized?  It
  800. generates an expose event whn it is enlarged, but not when it is shrunk.
  801.  
  802. Answer: Use the resize callback.
  803.  
  804. -----------------------------------------------------------------------------
  805. Subject: 77)  TOPIC: MENUS
  806.  
  807. -----------------------------------------------------------------------------
  808. Subject: 78)  What can I put inside a menu bar?
  809.  
  810. Answer: You can only put cascade buttons in menu bars. No pushbuttons, toggle
  811. buttons or gadgets are allowed. When you create a pulldown menu with parent a
  812. menu bar, its real parent is a shell widget.
  813.  
  814. -----------------------------------------------------------------------------
  815. Subject: 79)  Can I have a cascade button without a submenu in a pulldown
  816. menu?
  817.  
  818. Answer: Yes you can. A cascade button has an activate callback which is called
  819. when you click on it and it doesn't have a submenu. It can have a mnemonic,
  820. but keyboard traversal using the arrow keys in the menu will skip over it.
  821.  
  822. -----------------------------------------------------------------------------
  823. Subject: 80)  Should I have a cascade button without a submenu in a pulldown
  824. menu?
  825.  
  826. Answer: No. This is forbidden by the style guide. Technically you can do it
  827. (see previous question) but if you do it will not be Motif style compliant.
  828. This is unlikely to change - if a "button" is important enough to be in a
  829. pulldown menu bar with no pulldown, it should be a button elsewhere.  (Mind
  830. you, you won't be able to put accelerators on it elsewhere though.)
  831.  
  832. -----------------------------------------------------------------------------
  833. Subject: 81)  What is the best way to create popup menus?
  834. [Last modified: August 92]
  835.  
  836. Susan Murdock Thompson (from OSF): In general, create a popupMenu as the child
  837. from which you will be posting it from (ie: if you have a bulletinBoard with a
  838. PushButton in it and want MB2 on the pushButton to post the popupMenu, create
  839. the popupMenu as a child of the pushButton).  [This parent-child relationship
  840. seems to make a big difference in the behavior of the popups.]  Add an event
  841. handler to handle buttonPress events.  You'll need to check for the correct
  842. button (what you've specified menuPost to be) before posting the menu.
  843.  
  844. To create a popup that can be accessible from within an entire client window,
  845. create it as the child of the top-most widget (but not the shell) and add
  846. event handlers for the top-most widget and children widgets.
  847.  
  848. ie:
  849.  
  850. {
  851.   ....
  852.  
  853.   XtManageChild(rc=XmCreateRowColumn(Shell1, "rc", NULL, 0));
  854.   XtManageChild(label = XmCreateLabel(rc, "label", NULL, 0));
  855.   XtManageChild(text = XmCreateText(rc, "text", NULL, 0));
  856.   XtManageChild(pushbutton = XmCreatePushButton(rc, "pushbutton", NULL, 0));
  857.  
  858.   n = 0;
  859.   XtSetArg(args[n], XmNmenuPost, "<Btn3Down>"); n++;
  860.   popup = XmCreatePopupMenu(rc, "popup", args, n);
  861.  
  862.   XtAddEventHandler(rc, ButtonPressMask, False, PostMenu3, popup);
  863.   XtAddEventHandler(text, ButtonPressMask, False, PostMenu3, popup);
  864.   XtAddEventHandler(label, ButtonPressMask, False, PostMenu3, popup);
  865.   XtAddEventHandler(pushbutton, ButtonPressMask, False, PostMenu3, popup);
  866.  
  867.   XtManageChild(m1 = XmCreatePushButton(popup, "m1", NULL, 0));
  868.   XtManageChild(m2 = XmCreatePushButton(popup, "m2", NULL, 0));
  869.   XtManageChild(m3 = XmCreatePushButton(popup, "m3", NULL, 0));
  870.  
  871.   XtAddCallback(m1, XmNactivateCallback, SayCB, "button M1");
  872.   XtAddCallback(m2, XmNactivateCallback, SayCB, "button M2");
  873.   XtAddCallback(m3, XmNactivateCallback, SayCB, "button M3");
  874.   ...
  875. }
  876.  
  877. /* where PostMenu3 is ... */
  878.  
  879. PostMenu3 (w, popup, event)
  880. Widget w;
  881. Widget popup;
  882. XButtonEvent * event;
  883. {
  884.   printf("menuPost = 3, button %d0, event->button);
  885.  
  886.   if (event->button != Button3)
  887.     return;
  888.   XmMenuPosition(popup, event);
  889.   XtManageChild(popup);
  890. }
  891.  
  892.  
  893.  
  894. -----------------------------------------------------------------------------
  895. Subject: 82)  How do popup menus work?
  896. [Last modified: August 92]
  897.  
  898. Answer:
  899.  
  900. When a popup menu is created as the child of a widget the menu system installs
  901. a translation on the parent of the popup and descendants with an action which:
  902. (1) when 3-rd button (the default for the menuPost resource) is pressed the
  903. cursor changes and the mouse is grabbed for 5 seconds; (2) disables event
  904. handlers on the descendants and the handlers are never called; (3) an event
  905. handler installed on the parent works fine.
  906.  
  907. It is done so that the correct event handler will (in fact) be called.  There
  908. is a grab with owner_events true.  The grab is released by a timer,  but
  909. normally the posted menu shell puts up it's own grab.
  910.  
  911. If you only have widgets then you can use the subwindow field in the event to
  912. identify the original widget.  If you have gadgets or other data that you want
  913. to change the menu for (or use a specific menu for) then you must do a walk of
  914. the parent's children to find the best match.
  915.  
  916. One thing to beware of is that even with the grab,  because the menu system
  917. does a grab with owner events true, you must either have an event handler, or
  918. nothing that will use the event on each widget in the hierarchy of the menu's
  919. parent.  If a child widget has another event handler for button down, it may
  920. swallow the event and do something else.
  921.  
  922.  
  923.  
  924. -----------------------------------------------------------------------------
  925. Subject: 83)  Should I use translation tables or actions for popup menus?
  926. [Last modified: August 92]
  927.  
  928. Answer: The original goal of popupMenus was that the user would not have to
  929. specify an event handler to manage popupMenus; however, that did not become
  930. reality.  Larry Rogers wrote:
  931.  
  932. > There appear to be two ways to manage popup menus.  I
  933. > am curious what the correct way would be:
  934.  
  935. > 1.  Change the translation table of the widget with the
  936. >    popup child to popup the menu.  Note that this does
  937. >    not currently working for many widgets, because aug-
  938. >    menting their translations, even for augment breaks
  939. >    the widget.
  940.  
  941. > 2.  Add an event handler at creation to the widget; then
  942. >    determine if the event that caused the event handler
  943. >    to be called is the current button being used by the
  944. >    menu as its activation button.
  945.  
  946. Susan Murdock Thompson (from OSF) replied: *Theoretically, you should be able
  947. to do both.*  Our documentation says use event handlers.  Our tests for the
  948. toolkit use event handlers and for UIL use translations.  (Although I tried an
  949. event handler with a UIL test and it works).
  950.  
  951. -----------------------------------------------------------------------------
  952. Subject: 84)  What are the known bugs in popup menus?
  953. [Last modified: August 92]
  954.  
  955. Answer: As at Motif 1.1.4, the bugs for which an OSF PIR exists are:
  956.  
  957.    (3)  Menus not being sticky (ie: posted on a Btn CLICK)  [ Note:this
  958.         problem occurs with OptionMenus as well]  (PIR 3435)
  959.  
  960.    (6)  Destroying a widget with an associated popupMenu results in
  961.         "Warning: Attempt to remove non-existant passive grab"         (PIR
  962. 2972)
  963.  
  964.    (7)  Current documentation insufficient regarding requirements for
  965.         success in using PopupMenus.  (PIR 3433)
  966.  
  967.  
  968. -----------------------------------------------------------------------------
  969. Subject: 85)  Can I have multiple popup menus on the same widget?
  970. [Last modified: August 92]
  971.  
  972. Answer: If you want to have several popups (activated by different mouse
  973. buttons) on the same widget..., well, that doesn't work yet.
  974.  
  975. If you want to have several popups on different children... that works.  But
  976. don't put a popup on the parent (manager) widget, or it will rule!
  977.  
  978.  
  979.  
  980. -----------------------------------------------------------------------------
  981. Subject: 86)  TOPIC: INPUT FOCUS
  982.  
  983. -----------------------------------------------------------------------------
  984. Subject: 87) How can I specify the widget that should have the keyboard focus
  985. when my application starts up?  Answer: In Motif 1.2, use XmNinitialFocus on
  986. the manager widget.  thanks to Ken Lee, klee@synoptics.com
  987.  
  988.  
  989. -----------------------------------------------------------------------------
  990. Subject: 88)  How can I direct the keyboard input to a particular widget?
  991.  
  992. Answer: In Motif 1.1 call XmProcessTraversal(target, XmTRAVERSE_CURRENT).  The
  993. widget (and all of its ancestors) does need to be realized BEFORE you call
  994. this. Otherwise it has no effect.  XmProcessTraversal is reported to have many
  995. bugs, so it may not work right.  A common occurrence is that it doesn't move
  996. to the widget, but if you call XmProcessTraversal *twice* in a row, it will.
  997. If you can't get it to work, try this from Kee Hinckley:
  998.  
  999.     // This insane sequence is as follows:
  1000.     //      On manage set up a focus callback
  1001.     //      On focus callback set up a timer (and get rid of focus callback!)
  1002.     //      On timer set the focus (which only works if the parent
  1003.     //      has the focus,
  1004.     //      which is why we went through all of this garbage)
  1005.     // There may be a better way, but I haven't time to try it now.
  1006.     //
  1007.     static void focusTO(void *data, XtIntervalId *) {
  1008.         XmProcessTraversal((Widget) data, XmTRAVERSE_CURRENT);
  1009.     }
  1010.  
  1011.     static void focusCB(Widget w, XtPointer data, XtPointer) {
  1012.         XtRemoveCallback(w, XmNfocusCallback, focusCB, data);
  1013.         XtAppAddTimeOut(XtWidgetToApplicationContext(w), 0, focusTO, data);
  1014.     }
  1015.  
  1016.     void OmXSetFocus(Widget parent, Widget w) {
  1017.         XtAddCallback(parent, XmNfocusCallback, focusCB, w);
  1018.     }
  1019.  
  1020.  
  1021. In Motif 1.0 call the undocumented _XmGrabTheFocus(target).
  1022.  
  1023. Do not use the X or Xt calls such as XtSetKeyboardFocus since this bypasses
  1024. the Motif traversal layer and can cause it to get confused.  This can lead to
  1025. odd keyboard behaviour elsewhere in your application.
  1026.  
  1027. -----------------------------------------------------------------------------
  1028. Subject: 89)  How can I have a modal dialog which has to be answered before
  1029. the application can continue?
  1030. [Last modified: July 92]
  1031.  
  1032. Answer: The answer depends on whether you are using the Motif window manager
  1033. mwm or not.  Test for this by XmIsMotifWMRunning.
  1034.  
  1035. The window manager mwm knows how to control event passing to dialog widgets
  1036. declared as modal. If the dialog is set to application modal, then no
  1037. interaction with the rest of the application can occur until the dialog is
  1038. destroyed or unmanaged.
  1039.  
  1040. Use the appropriate code in the following program.  There is followup
  1041. discussion after the program.
  1042.  
  1043.  
  1044. /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  1045.  * This program is freely distributable without licensing fees and
  1046.  * is provided without guarantee or warranty expressed or implied.
  1047.  * This program is -not- in the public domain.  This program is
  1048.  * taken from the Motif Programming Manual, O'Reilly Volume 6.
  1049.  */
  1050.  
  1051. /*
  1052.  * ask_user.c -- create a pushbutton that posts a dialog box
  1053.  * that asks the user a question that requires an immediate
  1054.  * response.  The function that asks the question actually
  1055.  * posts the dialog that displays the question, waits for and
  1056.  * returns the result.
  1057.  */
  1058. #include <X11/Intrinsic.h>
  1059. #include <Xm/DialogS.h>
  1060. #include <Xm/SelectioB.h>
  1061. #include <Xm/RowColumn.h>
  1062. #include <Xm/MessageB.h>
  1063. #include <Xm/PushBG.h>
  1064. #include <Xm/PushB.h>
  1065.  
  1066. XtAppContext app;
  1067.  
  1068. #define YES 1
  1069. #define NO  2
  1070.  
  1071. /* main() --create a pushbutton whose callback pops up a dialog box */
  1072. main(argc, argv)
  1073. char *argv[];
  1074. int argc;
  1075. {
  1076.     Widget parent, button, toplevel;
  1077.     XmString label;
  1078.     void pushed();
  1079.  
  1080.     toplevel = XtAppInitialize(&app, "Demos",
  1081.         NULL, 0, &argc, argv, NULL, NULL, 0);
  1082.  
  1083.     label = XmStringCreateSimple("/bin/rm *");
  1084.     button = XtVaCreateManagedWidget("button",
  1085.         xmPushButtonWidgetClass, toplevel,
  1086.         XmNlabelString,          label,
  1087.         NULL);
  1088.     XtAddCallback(button, XmNactivateCallback,
  1089.         pushed, "Remove Everything?");
  1090.     XmStringFree(label);
  1091.  
  1092.     XtRealizeWidget(toplevel);
  1093.     XtAppMainLoop(app);
  1094. }
  1095.  
  1096. /* pushed() --the callback routine for the main app's pushbutton. */
  1097. void
  1098. pushed(w, question)
  1099. Widget w;
  1100. char *question;
  1101. {
  1102.     if (AskUser(w, question) == YES)
  1103.         puts("Yes");
  1104.     else
  1105.         puts("No");
  1106. }
  1107.  
  1108. /*
  1109.  * AskUser() -- a generalized routine that asks the user a question
  1110.  * and returns the response.
  1111.  */
  1112. AskUser(parent, question)
  1113. char *question;
  1114. {
  1115.     static Widget dialog;
  1116.     XmString text, yes, no;
  1117.     static int answer;
  1118.     extern void response();
  1119.  
  1120.     answer = 0;
  1121.     if (!dialog) {
  1122.         dialog = XmCreateQuestionDialog(parent, "dialog", NULL, 0);
  1123.         yes = XmStringCreateSimple("Yes");
  1124.         no = XmStringCreateSimple("No");
  1125.         XtVaSetValues(dialog,
  1126.             XmNdialogStyle,        XmDIALOG_APPLICATION_MODAL,
  1127.             XmNokLabelString,      yes,
  1128.             XmNcancelLabelString,  no,
  1129.             NULL);
  1130.         XtSetSensitive(
  1131.             XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON), False);
  1132.         XtAddCallback(dialog, XmNokCallback, response, &answer);
  1133.         XtAddCallback(dialog, XmNcancelCallback, response, &answer);
  1134.         /* if the user interacts via the system menu: */
  1135.         XtAddCallback(dialog, XmNpopdownCallback, response, &answer);
  1136.     }
  1137.     text = XmStringCreateSimple(question);
  1138.     XtVaSetValues(dialog,
  1139.         XmNmessageString,      text,
  1140.         NULL);
  1141.     XmStringFree(text);
  1142.     XtManageChild(dialog);
  1143.     XtPopup(XtParent(dialog), XtGrabNone);
  1144.  
  1145.     /* while the user hasn't provided an answer, simulate XtMainLoop.
  1146.      * The answer changes as soon as the user selects one of the
  1147.      * buttons and the callback routine changes its value.  Don't
  1148.      * break loop until XtPending() also returns False to assure
  1149.      * widget destruction.
  1150.      */
  1151.     while (answer == 0 || XtAppPending(app))
  1152.         XtAppProcessEvent(app, XtIMAll);
  1153.     return answer;
  1154. }
  1155.  
  1156. /* response() --The user made some sort of response to the
  1157.  * question posed in AskUser().  Set the answer (client_data)
  1158.  * accordingly and destroy the dialog.
  1159.  */
  1160. void
  1161. response(w, answer, reason)
  1162. Widget w;
  1163. int *answer;
  1164. XmAnyCallbackStruct *reason;
  1165. {
  1166.     switch (reason->reason) {
  1167.         case XmCR_OK:
  1168.             *answer = YES;
  1169.             break;
  1170.         case XmCR_CANCEL:
  1171.             *answer = NO;
  1172.             break;
  1173.         default:
  1174.             *answer = NO;
  1175.             return;
  1176.     }
  1177. }
  1178.  
  1179.  
  1180.  
  1181. If you aren't running a window manager that acknowledges this hint, then you
  1182. may have to grab the pointer (and keyboard) yourself to make sure the user
  1183. doesn't interact with any other widget.  Change the grab flag in XtPopup to
  1184. XtGrabExclusive, and XtRemoveGrab(XtParent(w)) to the response() function.
  1185.  
  1186.  
  1187. -----------------------------------------------------------------------------
  1188. Subject: 90)  TOPIC: MEMORY AND SPEED
  1189.  
  1190. -----------------------------------------------------------------------------
  1191. Subject: 91)  When can I free data structures passed to or retrieved from
  1192. Motif?
  1193.  
  1194. Answer:
  1195.  In most cases, especially for XmStrings and XmFontLists, Motif copies data
  1196. passed to it or retrieved from it, so it may be freed immediately.  Server-
  1197. side resources, such as pixmaps and color cells, however, are not copied, so
  1198. should not be freed.  More recent versions of Motif are better than earlier
  1199. versions and exceptions should be documented.  thanks to klee*synoptics.com
  1200. (Ken Lee)
  1201.  
  1202. -----------------------------------------------------------------------------
  1203. Subject: 92)  Why does my application grow in size?
  1204.  
  1205. Answer: Motif 1.0 has many memory leaks, particularly in XmString
  1206. manipulation.  Switch to Motif 1.1.
  1207.  
  1208. Answer: The Intrinsics have a memory leak in accelerator table management, and
  1209. Motif uses this heavily.  Avoid this by mapping/unmapping widgets rather than
  1210. creating/destroying them, or get  X11R4 fix-15/16/17.
  1211.  
  1212. Answer: The server may grow in size due to its own memory leaks.  Switch to a
  1213. later server.
  1214.  
  1215. Answer: You are responsible for garbage collection in `C'.  Some common cases
  1216. where a piece of memory becomes garbage are
  1217.  
  1218.  a.  Memory is allocated by Motif for XmStrings by the functions
  1219.      XmStringConcat, XmStringCopy, XmStringCreate, XmStringCreateLtoR,
  1220.      XmStringCreateSimple, XmStringDirectionCreate, XmStringNConcat,
  1221.      XmStringNCopy, XmStringSegmentCreate, and XmStringSeparatorCreate.  The
  1222.      values returned by these functions should be freed using XmStringFree
  1223.      when they are no longer needed.
  1224.  
  1225.  b.  Memory is allocated by Motif for ordinary character strings (of type
  1226.      String) by Motif in XmStringGetLtoR, XmStringGetNextComponent, and
  1227.      XmStringGetNextSegment. After using the string, XtFree() it. [Note that
  1228.      XmStrings and Strings are two different data types.  XmStrings are
  1229.      XmStringFree'd, Strings are XtFree'd.]
  1230.  
  1231.  c.  If you have set the label (an XmString) in a label, pushbutton, etc
  1232.      widget, free it after calling XtSetValues() or the widget creation
  1233.      routine by XmStringFree().
  1234.  
  1235.  d.  If you have set text in a text widget, the text widget makes its own
  1236.      copy.  Unless you have a use for it, there is no need to keep your own
  1237.      copy.
  1238.  
  1239.  e.  If you have set the strings in a list widget the list widget makes its
  1240.      own copy.  Unless you have a use for it, there is no need to keep your
  1241.      own copy.
  1242.  
  1243.  f.  When you get the value of a single compound string from a Widget e.g.
  1244.      XmNlabelString, XmNmessageString, ... Motif gives you a copy of its
  1245.      internal value.  You should XmStringFree this when you have finished with
  1246.      it.
  1247.  
  1248.  g.  On the other hand, when you get a value of a Table e.g. XmStringTable for
  1249.      a List, you get a *pointer* to the internal Table, and should not free
  1250.      it.
  1251.  
  1252.  h.  When you get the value of the text in a widget by XmTextGetString or from
  1253.      the resource XmNvalue, you get a copy of the text.  You should XtFree
  1254.      this when you have finished with it.
  1255.  
  1256. Answer: From Josef Nelissen: at least in Motif 1.1.4, X11R4 on a HP 720, the
  1257. XmText/XmTextFieldSetString() functions have a memory leak.  The old
  1258. value/contents of the Widget isn't freed correctly.  To work around this bug,
  1259. one should use a XmText Widget (in single-line-mode) instead of a XmTextField
  1260. Widget (the solution fails with XmTextField Widgets !) and replace any
  1261.  
  1262.        XmTextSetString(text_widget, str);
  1263.  
  1264. by
  1265.  
  1266.        XmTextReplace(text_widget, (XmTextPosition) 0,
  1267.                      XmTextGetLastPosition(text_widget), str);
  1268.  
  1269.  
  1270. -----------------------------------------------------------------------------
  1271. Subject: 93)  Why does my application take a long time to start up?
  1272.  
  1273. Answer: You are probably creating too many widgets at startup time.  Delay
  1274. creating them until needed.  If you have a large number of resources in text
  1275. files (such as in app-defaults), time may be spent reading and parsing it.
  1276.  
  1277. -----------------------------------------------------------------------------
  1278. Subject: 94)  My application is running too slowly. How can I speed it up?
  1279.  
  1280. Answer: Use the R4 rather than R3 server.  It is much faster.
  1281.  
  1282. Answer: The standard memory allocator is not well tuned to Motif, and can
  1283. degrade performance.  Use a better allocator.  e.g. with SCO Unix, link with
  1284. libmalloc.a; use the allocator from GNU emacs; use the allocator from Perl.
  1285.  
  1286. Answer: Avoid lots of widget creation and destruction.  It fragments memory
  1287. and slows everything down.  Popup/popdown, manage/unmanage instead.
  1288.  
  1289. Answer: Set mappedWhenManaged to FALSE, and then call XtMapWidget()
  1290. XtUnmapWidget() rather than managing.
  1291.  
  1292. Answer: Get more memory - your application, the server and the Operating
  1293. System may be spending a lot of time being swapped.
  1294.  
  1295. Answer: If you are doing much XmString work yourself, such as heavy use of
  1296. XmStringCompare, speed may deteriorate due to the large amount of internal
  1297. conversions and malloc'ing.  Try using XmStringByteCompare if appropriate or
  1298. ordinary Ascii strings if you can.
  1299.  
  1300.  
  1301.  
  1302. -----------------------------------------------------------------------------
  1303. Subject: 95)  Why is my application so huge?
  1304.  
  1305. Answer: The typical size of a statically linked Motif app is in the megabytes.
  1306. This is often caused by the size of libXm.a. A large part of this gets linked
  1307. in to even trivial Motif programs. You can reduce the code size by linking
  1308. against shared libraries if they are available.  Running "strip" on the
  1309. executable can often reduce size. Note that the size of the running program
  1310. should be measured by "ps", not by the code size.
  1311.  
  1312. -----------------------------------------------------------------------------
  1313. END OF PART THREE
  1314. -- 
  1315. ..........................
  1316.  
  1317.  
  1318.